|
1
|
|
|
import * as ID3Util from "./ID3Util" |
|
2
|
|
|
import { isString } from "./util" |
|
3
|
|
|
import { TextEncoding } from "./definitions/Encoding" |
|
4
|
|
|
|
|
5
|
|
|
type Value = Buffer | number | string |
|
6
|
|
|
|
|
7
|
|
|
export class FrameBuilder { |
|
8
|
|
|
private identifier: string |
|
9
|
|
|
private buffer = Buffer.alloc(0) |
|
10
|
|
|
|
|
11
|
|
|
constructor(identifier: string) { |
|
12
|
|
|
this.identifier = identifier |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
appendValue( |
|
16
|
|
|
value: Value, |
|
17
|
|
|
size?: number | null, |
|
18
|
|
|
encoding: TextEncoding = TextEncoding.ISO_8859_1 |
|
19
|
|
|
) { |
|
20
|
|
|
const convertedValue = convertValue(value, encoding) |
|
21
|
|
|
this.appendBuffer(staticValueToBuffer(convertedValue, size)) |
|
22
|
|
|
return this |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
appendNumber(value: number, size: number) { |
|
26
|
|
|
if (Number.isInteger(value)) { |
|
27
|
|
|
let hexValue = value.toString(16) |
|
28
|
|
|
if (hexValue.length % 2 !== 0) { |
|
29
|
|
|
hexValue = "0" + hexValue |
|
30
|
|
|
} |
|
31
|
|
|
this.appendBuffer( |
|
32
|
|
|
staticValueToBuffer(Buffer.from(hexValue, 'hex'), size) |
|
33
|
|
|
) |
|
34
|
|
|
} |
|
35
|
|
|
return this |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
appendNullTerminatedValue( |
|
39
|
|
|
value = '', |
|
40
|
|
|
encoding: TextEncoding = TextEncoding.ISO_8859_1 |
|
41
|
|
|
) { |
|
42
|
|
|
this.appendBuffer( |
|
43
|
|
|
convertValue(value, encoding), |
|
44
|
|
|
getTerminatingMarker(encoding) |
|
45
|
|
|
) |
|
46
|
|
|
return this |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
getBuffer() { |
|
50
|
|
|
const header = Buffer.alloc(10) |
|
51
|
|
|
header.write(this.identifier, 0) |
|
52
|
|
|
header.writeUInt32BE(this.buffer.length, 4) |
|
53
|
|
|
return Buffer.concat([header, this.buffer]) |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private appendBuffer(...buffers: Buffer[]) { |
|
57
|
|
|
this.buffer = Buffer.concat([this.buffer, ...buffers]) |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
function convertValue( |
|
62
|
|
|
value: Value, |
|
63
|
|
|
encoding: TextEncoding = TextEncoding.ISO_8859_1 |
|
64
|
|
|
) { |
|
65
|
|
|
if (value instanceof Buffer) { |
|
66
|
|
|
return value |
|
67
|
|
|
} |
|
68
|
|
|
if (Number.isInteger(value) || isString(value)) { |
|
69
|
|
|
return ID3Util.stringToEncodedBuffer(value.toString(), encoding) |
|
70
|
|
|
} |
|
71
|
|
|
return Buffer.alloc(0) |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function staticValueToBuffer(buffer: Buffer, size?: number | null) { |
|
75
|
|
|
if (size && buffer.length < size) { |
|
76
|
|
|
return Buffer.concat([Buffer.alloc(size - buffer.length, 0x00), buffer]) |
|
77
|
|
|
} |
|
78
|
|
|
return buffer |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function getTerminatingMarker(encoding: TextEncoding) { |
|
82
|
|
|
if (encoding === TextEncoding.UTF_16_WITH_BOM || |
|
83
|
|
|
encoding === TextEncoding.UTF_16_BE |
|
84
|
|
|
) { |
|
85
|
|
|
return Buffer.alloc(2, 0x00) |
|
86
|
|
|
} |
|
87
|
|
|
return Buffer.alloc(1, 0x00) |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|